package cn.edu.lcu.cs.javaprogramming.basic;

import java.util.Scanner;

/**
 * <h3>统计成绩，用While循环实现</h3>
 * <ol>
 *     <li>从控制台循环读取84个Java课程的成绩，成绩为0-100之间的整数，如果成绩非法，输入提前结束。</li>
 *     <li>统计各分数段的人数。优、良、中、及格、不及格。</li>
 *     <li>结果打印到控制台。</li>
 * </ol>
 *
 * @author ling
 */
public class StatisticalScores2 {
    public static void main(String[] args) {
        System.out.println("请输入0-100之间的成绩");
        Scanner scanner = new Scanner(System.in);

        int gradeA = 0;
        int gradeB = 0;
        int gradeC = 0;
        int gradeD = 0;
        int gradeE = 0;

        // 学生人数
        int count = 4;
        int i = 0;
        while (i++ < count) {
            //i++;
            int score = scanner.nextInt();
            if (score < 0 || score > 100) {
                break;
            }

            // 用else if语句多次判断
            //if (score < 60) {
            //    gradeE++;
            //} else if (score < 70) {
            //    gradeD++;
            //} else if (score < 80) {
            //    gradeC++;
            //} else if (score < 90) {
            //    gradeB++;
            //} else {
            //    gradeA++;
            //}

            // 本例比较特殊，也可用Switch语句实现
            switch (score / 10) {
                case 10:
                case 9:
                    gradeA++;
                    break;
                case 8:
                    gradeB++;
                    break;
                case 7:
                    gradeC++;
                    break;
                case 6:
                    gradeD++;
                    break;
                default:
                    gradeE++;
                    break;
            }

            //也可用Java12中引入的增强Switch语法实现
            /*
            switch (score / 10) {
                case 10, 9 -> gradeA++;
                case 8 -> gradeB++;
                case 7 -> gradeC++;
                case 6 -> gradeD++;
                default -> gradeE++;
            }
            */

        }

        System.out.printf("优%d人，良%d人，中%d人，差%d人，不及格%d人", gradeA, gradeB, gradeC, gradeD, gradeE);

    }
}
